'''
    @Author:webrainer
    @Date:2023.05.26
    @Version:
    @Since:python3.11
    @See:
    @Description:日志模块，可以用于记录日志，在我实现的日志中，debug为蓝色字体,info为黑色字体,warning为黄色字体,error为红色字体，critical为红色字体加粗
    @Functions:debug(),info(),warning(),error(),critical()

    使用示例
    # 导入模块
    from utils.log import Logger
    logger = Logger()
    logger.debug("This is a debug message")
    logger.info("This is an info message")
    logger.warning("This is a warning message")
    logger.error("This is an error message")
    logger.critical("This is a critical message")

    # 测试
    if __name__ == '__main__':
        logger = Logger()
        logger.debug("This is a debug message")
        logger.info("This is an info message")
        logger.warning("This is a warning message")
        logger.error("This is an error message")
        logger.critical("This is a critical message")
'''
import logging


# 通过实现handler类，可以实现自定义的日志输出格式
class ColoredFormatter(logging.Formatter):
    def __init__(self, fmt=None, datefmt=None, style='%'):
        super().__init__(fmt=fmt, datefmt=datefmt, style=style)
        # 设置日志级别对应的颜色
        self._level_to_color = {
            logging.DEBUG: '\033[34m',  # 蓝色
            logging.INFO: '\033[0m',  # 黑色
            logging.WARNING: '\033[33m',  # 黄色
            logging.ERROR: '\033[31m',  # 红色
            logging.CRITICAL: '\033[31m\033[1m'  # 红色加粗
        }

    def format(self, record):
        # 根据日志级别，选择对应的颜色
        record.msg= '{color_begin}{message}{color_end}'.format(
            color_begin=self._level_to_color[record.levelno],
            message=record.msg,
            color_end='\033[0m'
        )
        record.levelname = '{color_begin}{level}{color_end}'.format(
            color_begin=self._level_to_color[record.levelno],
            level=record.levelname,
            color_end='\033[0m'
        )
        return super().format(record)


class Logger:
    def __init__(self, log_file=None):
        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(logging.DEBUG)
        # 设置日志输出格式,使用上面的colored_formatter
        formatter = ColoredFormatter("%(asctime)s - %(levelname)s - %(message)s")
        # 创建控制台处理器
        console_handler = logging.StreamHandler()
        console_handler.setLevel(logging.DEBUG)
        console_handler.setFormatter(formatter)
        self.logger.addHandler(console_handler)

        if log_file:
            # 创建文件处理器
            file_handler = logging.FileHandler(log_file)
            file_handler.setLevel(logging.DEBUG)
            file_handler.setFormatter(formatter)
            self.logger.addHandler(file_handler)

    def debug(self, message):
        self.logger.debug(message)

    def info(self, message):
        self.logger.info(message)

    def warning(self, message):
        self.logger.warning(message)

    def error(self, message):
        self.logger.error(message)

    def critical(self, message):
        self.logger.critical(message)


# test
if __name__ == '__main__':
    logger = Logger()
    logger.debug("This is a debug message")
    logger.info("This is an info message")
    logger.warning("This is a warning message")
    logger.error("This is an error message")
    logger.critical("This is a critical message")
